package com.example.ojcodesandbox.rabbitmq;

import cn.hutool.json.JSONUtil;
import com.example.ojcodesandbox.codesandbox.CodeSandboxFactory;
import com.example.ojcommon.common.ExecuteCodeRequest;
import com.example.ojcommon.common.ExecuteCodeResponse;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class QuestionListener {

    @Resource
    private CodeSandboxFactory codeSandboxContext;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = {"questionQueue"})
    @RabbitHandler
    public void listen(ExecuteCodeRequest executeCodeRequest) {
        //接受到消息，向题目服务发送消息修改题目状态
        rabbitTemplate.convertAndSend("questionExchange", "update", executeCodeRequest.getSubmitId());
        //执行代码
        ExecuteCodeResponse executeCodeResponse = codeSandboxContext.execute(executeCodeRequest);
        rabbitTemplate.convertAndSend("questionExchange", "return", executeCodeResponse);
    }
}
